#pragma once
#include "noncopyable.h"
#include "Socket.h"
#include "Channel.h"

#include <functional>

class EventLoop;
class InetAddress;

class Acceptor : noncopyable
{
public:
    using NewConnectionCallBack = std::function<void(int, const InetAddress &)>;

    Acceptor(EventLoop *loop, const InetAddress &listenAddr, bool reuseport);
    ~Acceptor();

    void setNewConnectionCallBack(const NewConnectionCallBack &cb)
    {
        newConnectionCallBack_ = cb;
    }

    bool listening() const
    {
        return listening_;
    }

    void listen();

private:
    void handleRead(); // accept read事件回调

    EventLoop *loop_;                             // baseLoop
    Socket acceptSocket_;                         // 套接字
    Channel acceptChannel_;                       // 操作loop上event
    NewConnectionCallBack newConnectionCallBack_; // 创建新链接回调函数
    bool listening_;                              // 记录监听状态
};